package com.fastshopping.order.controller;

import com.fastshopping.order.pojo.Order;
import com.fastshopping.order.service.OrderService;
import com.wink.entity.PageResult;
import com.wink.entity.QueryPageBean;
import com.wink.entity.Result;
import com.wink.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Name: 李袁勇
 * @Date: 2020/6/2 16:36
 * @Version: 1.0
 **/
/*
*
* 订单列表接口*/

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /*
    * 分页查询，分页条件查询*/
    @RequestMapping("/findAll")
    public PageResult findAllOrder(Integer currentPage,Integer pageSize,String queryString,@RequestBody Order order){
        QueryPageBean queryPageBean = new QueryPageBean(pageSize,currentPage,queryString);
        if(queryPageBean.getPageSize() == null){
            queryPageBean.setPageSize(5);
        }
        if(queryPageBean.getCurrentPage() == null){
            queryPageBean.setCurrentPage(0);
        }
        System.out.println(order.getDelivery());
        PageResult all = orderService.findAll(queryPageBean,order);
        return all;
    }

    /*
    * 统计每一种订单状态的数量*/
    @RequestMapping("/findCount")
    public Result findCout(){
        try {
            List<Map> count = orderService.findCount();
            return new Result(true,StatusCode.OK,"统计成功",count);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.REPERROR,"发生错误");
        }
    }

    /*
    * 查询当前订单详情*/
    @RequestMapping("/findOneOrder")
    public Result findOneOrder(int id){
        try {
            Order oneOrder = orderService.findOneOrder(id);
            return new Result(true,StatusCode.OK,"查询成功",oneOrder);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,StatusCode.REPERROR,"发生错误");
    }

    /*
    * 取消当前订单*/
    @RequestMapping("/cancelOrder")
    public Result updateById(int id){
        try {
            orderService.updateById(id);
            return new Result(true,StatusCode.OK,"取消订单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.REPERROR,"取消订单失败，请稍后重试");
        }
    }

    /*
    * 确认发货*/
    @RequestMapping("/deliverGoods")
    public Result deliverGoods(int id){
        try {
            orderService.deliverGoods(id);
            return new Result(true,StatusCode.OK,"发货成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.REPERROR,"发货失败，请稍后重试");
        }
    }

    /*
    * 确认当前货物提货(order.takeGoods)*/
    @RequestMapping("/takeGoods")
    public Result takeGoods(int id){
        try {
            orderService.takeGoods(id);
            return new Result(true,StatusCode.OK,"确认自提成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.REPERROR,"确认自提失败，请稍后重试");
        }
    }

    /*
    * 确认收货*/

    @RequestMapping("/confirmReceipt")
    public Result confirmReceipt(int id){
        try {
            orderService.confirmReceipt(id);
            return new Result(true,StatusCode.OK,"确认收货成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.REPERROR,"确认收货失败失败，请稍后重试");
        }
    }

    /*
    * 查看发货单*/


    /*
    * 退款*/


}
